package KWIC;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 批处理风格
 */
public class KWIC {
    public static void main(String[] args) {
        List<String[]> tempList1_1 = getContext(new File("/Users/fengke/Downloads/学习资料/软件体系结构/input.txt"));
        List<String[]> input2List = copyList(tempList1_1);
        String input2 = "welcome to chengdu";
        input2List.add(input2.split(" "));
        //写input2
        writeFile(input2List, "input2.txt");
        List<String[]> tempList1_2 = loopShift(tempList1_1);
        List<String[]> tempList1_3 = sort(tempList1_2);
        writeFile(tempList1_3, "output.txt");

        List<String[]> tempList2_1 = getContext(new File("/Users/fengke/Downloads/学习资料/软件体系结构/input2.txt"));
        List<String[]> tempList2_2 = loopShift(tempList2_1);
        List<String[]> tempList2_3 = sort(tempList2_2);
        writeFile(tempList2_3, "output2.txt");
    }

    /**
     * 文件读取
     *
     * @param file
     */
    private static List<String[]> getContext(File file) {
        //strList 初始列表，行数据 一行一个节点，单词间用空格分割
        List<String[]> strList = new ArrayList<>();
        try {
            // 1.系统从文本文件中读入字符串
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            //循环读取行
            while ((line = reader.readLine()) != null) {
                //以空格为分隔符
                strList.add(line.split(" "));
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("初始列表 strList:");
        printf(strList);
        return strList;
    }

    /**
     * 循环移位操作
     *
     * @param strList
     */
    private static List<String[]> loopShift(List<String[]> strList) {
        List<String[]> shiftStrList = new ArrayList<>();
        // 处理行数据
        for (String[] strings : strList) {
            for (int i = 0; i < strings.length; i++) {
                StringBuilder builder = new StringBuilder();
                int j = i;

                for (int times = 0; times < strings.length; times++) {
                    builder.append(strings[j]).append(",");
                    j++;
                    if (j == strings.length) {
                        j = 0;
                    }
                }
                shiftStrList.add(builder.toString().split(","));
            }
        }
        System.out.println("移位列表 shiftStrList:");
        printf(shiftStrList);
        //排序
//        sort(shiftStrList);
        return shiftStrList;
    }

    /**
     * 排序
     *
     * @param shiftStrList
     */
    private static List<String[]> sort(List<String[]> shiftStrList) {

        shiftStrList.sort(Comparator.comparing(o -> o[0]));
        //输出
        System.out.println("排序列表 endList");
        printf(shiftStrList);
        return shiftStrList;
    }

    private static List<String[]> copyList(List<String[]> copyList) {
        List<String[]> returnList = new ArrayList<>();
        for (String[] strings : copyList) {
            String[] tempList = new String[strings.length];
            for (int i=0; i<strings.length; i++) {
                tempList[i] = strings[i];
            }
            returnList.add(tempList);
        }
        return returnList;
    }

    /**
     * 写文件
     *
     * @param writeList
     */
    private static void writeFile(List<String[]> writeList, String fileName) {
        // 7.写新文件
        try {
            //创建文件
            File testFile = new File("/Users/fengke/Downloads/学习资料/软件体系结构/" + fileName);
            if (!testFile.exists()) {
                testFile.createNewFile();//有路径才能创建文件
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(testFile));
            for (String[] strings : writeList) {
                for (int i=0 ; i<strings.length ; i++) {
                    if (i<strings.length-1) {
                        out.write(strings[i]);
                        out.write(" ");
                    } else if (i == strings.length-1) {
                        out.write(strings[i]);
                    }
                }
                out.newLine();
            }
            out.close();
        } catch (IOException e) {
        }

    }

    private static void printf(List<String[]> result) {

        for (String[] strings : result) {
            for (String string : strings) {
                System.out.printf(string + " ");
            }
            System.out.println();
        }
    }

}
